﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;

public partial class Dashboard : System.Web.UI.Page
{
    SqlConnection CNx = new SqlConnection();
    SqlConnection CNx2 = new SqlConnection();
    string IDU, NDU;
    string Valor;
    Utilitarios util = new Utilitarios();

    protected void Page_Load(object sender, EventArgs e)
    {
        string ff;
        DateTime Fh =DateTime.Now;
        int hora = Fh.Hour;
        string quehora;
        if (hora >= 6 & hora < 12) quehora = "Buenos dias";
        else if (hora >= 12 & hora < 19) quehora = "Buenas tardes";
        else quehora = "Buenas noches";

        

        ff = Fh.ToLongDateString();
        
        lblFecha.Text = ff; // "Sabado 12 de octubre del 2014";

        if (!IsPostBack)
        {
            if (Session["IDUsuario"] == null)
            {
                Response.Redirect("Default.aspx");
            };
            
        }
        IDU= Session["IDUsuario"].ToString();
        NDU = Session["NUsuario"].ToString();
        lblUsuario.Text = quehora + " " + NDU;

        CNx.ConnectionString = Utilitarios.Conexion;
        CNx2.ConnectionString = Utilitarios.Conexion2;
        llenardatos();
    }

    protected void btnClAceptar_Click(object sender, EventArgs e)
    {
        String Msg = "Select clave from SIS_Usuarios where IDU=" + IDU;
        CNx.Open();
        SqlCommand Tb = new SqlCommand(Msg, CNx);
        String Clave = Tb.ExecuteScalar().ToString();

        Encrypt ClaseEncriptado = new Encrypt();
        String Resultado = ClaseEncriptado.EncryptKey(txtClAnterior.Text);
        if (Resultado != Clave)
        {
            //ClientScript.RegisterStartupScript(GetType(), "mostraralerta", "mostrarmsg('Cambiar clave', 'Error en la contraseña', 1)", true);
            ClientScript.RegisterStartupScript(GetType(), "mostraralerta", "alert('Error en la contraseña')",true);
            return;
        };
        Resultado = ClaseEncriptado.EncryptKey(txtClNueva.Text);
        Msg = "Update SIS_Usuarios set clave = '" + Resultado + "' where IDU ="+ IDU;
        Tb = new SqlCommand(Msg, CNx);
        Tb.ExecuteNonQuery();

        CNx.Close();
        //ClientScript.RegisterStartupScript(GetType(), "mostraralerta", "mostrarmsg('Cambiar clave', 'Se cambio con exito la contraseña', 2)", true);
        //ClientScript.RegisterStartupScript(GetType(), "mostraralerta", "alert('Se cambio con exito la contraseña')", true);
    }

    protected void llenardatos()
    {
    //    CNx.Open();
    //    string qry = "";


    //    CNx.Close();

        CNx2.Open();
        PorVencer();
        Ingresos();
        CNx2.Close();
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {

    }

    private void Ingresos()
    {
        int Ctos = 0;
        DateTime FF2 = DateTime.Today;
        string filtro = "Select * from LEG_VActs where FechaReg > @f1";
        SqlCommand Tb = new SqlCommand(filtro, CNx2);
        Tb.Parameters.Add("@f1", SqlDbType.DateTime).Value = FF2;
        SqlDataReader Dr = Tb.ExecuteReader();
        while (Dr.Read())
        {
            HyperLink IpB = new HyperLink();
            DateTime dd = (DateTime)Dr["FechaExped"];
            string Texto = Dr["NroExped"].ToString()+ "("+dd.ToString("dd/MM/yyyy")+")";
            string Sumi = Dr["Sumilla"].ToString();
            if (Sumi.Length > 50) Sumi = Sumi.Substring(0, 50) + "...";
            //IpB.NavigateUrl = "Dashboard.aspx?idc=" + Texto;
            IpB.CssClass = "lista_grupo";
            string txtdato = "<h4 class='list-group-item-heading' style='line-height:15px;'>" + Texto  + "</h4>";  
            txtdato = txtdato + "<p class='list-group-item-text' style='Font-size:15px;line-height:15px;'>" + Sumi + "</p>";
            IpB.Text = txtdato;
            areadatos.Controls.Add(IpB);
            Ctos++;
        }
        Dr.Close();
        lblIH.Text  = util.Ceros(Ctos,2);
    }

    private void PorVencer()
    {
        DateTime FF1 = DateTime.Today;
        FF1 =  new DateTime(FF1.Year, FF1.Month, FF1.Day, 0, 0, 0);
        DateTime FF2 = FF1.AddDays(7);
        FF2 = new DateTime(FF2.Year, FF2.Month, FF2.Day, 23, 59, 59);
        int Ctos = 0;
        string filtro = "Select * from LEG_VActs where IDEx in (select IDExp from leg_actuados where EstVcmto=1 and FechaVcmto between @f1 AND @f2)";
        SqlCommand Tb = new SqlCommand(filtro, CNx2);
        Tb.Parameters.Add("@f1", SqlDbType.DateTime).Value = FF1;
        Tb.Parameters.Add("@f2", SqlDbType.DateTime).Value = FF2;
        SqlDataReader Dr = Tb.ExecuteReader();
        while (Dr.Read())
        {
            HyperLink IpB = new HyperLink();
            DateTime dd = (DateTime)Dr["FechaExped"];
            DateTime dv = (DateTime)Dr["FechaVcmto"];
            string Texto = Dr["NroExped"].ToString() + "(" + dd.ToString("dd/MM/yyyy") + ")   Vencimiento: " + dv.ToString("dd/MM/yyyy");
            string Sumi = Dr["Sumilla"].ToString();
            if (Sumi.Length > 50) Sumi = Sumi.Substring(0, 50) + "....";
            //IpB.NavigateUrl = "Dashboard.aspx?idc=" + Texto;
            IpB.CssClass = "lista_grupo";
            string txtdato = "<h4 class='list-group-item-heading' style='line-height:15px;'>" + Texto + "</h4>";
            txtdato = txtdato + "<p class='list-group-item-text' style='Font-size:15px;line-height:15px;'>" + Sumi + "</p>";
            IpB.Text = txtdato;
            areadatos2.Controls.Add(IpB);
            Ctos++;
        }
        Dr.Close();
        lblPV.Text = util.Ceros(Ctos,2);
        //string msg = "Select * from
        ////while (Dr.Read())
        ////{
        ////    //cta++;
        //HyperLink IpB = new HyperLink();
        ////Texto = Dr["IDContrato"].ToString();
        ////    //IpB.NavigateUrl = "Dashboard.aspx?idc=" + Texto;
        //IpB.CssClass = "lista_grupo";
        //string txtdato = "<h4 class='list-group-item-heading' style='line-height:15px;'>" + "11003-2015" + " " + "(01/13/2015)" + "</h4>";
        //txtdato = txtdato + "<p class='list-group-item-text' style='Font-size:15px;line-height:15px;'>" + "Problemas de coordinacion local" + "</p>";
        //IpB.Text = txtdato;
        //areadatos.Controls.Add(IpB);

    }

 }